<?php

namespace Home\Controller;
use Think\Controller;
//header("Content-type:text/html;charset=gb2312");
set_time_limit(0);
//ini_set('memory_limit', '256m');

class SpiderController extends Controller{
	
	
	public function get_dyzz(){
		$URL = C("WEB_URL");
		echo ($time = time())."<br>";
		$link = $URL."/html/gndy/dyzz/list_23";
		$num = $this->get_page_num($link);
		$time1 = time();
		echo "抓取页面数量用时".($time1-$time)."s<br />";
		$type = 1;
		$res = $this->insert_content($link,$num,$type);
		//echo count($res)."<br />";
		$time2 = time();
		echo $time = "共用时".($time2-$time)."s";
		//print_r($res);
		//unset($res);
	}
	public function get_china(){
		$URL = C("WEB_URL");
		echo ($time = time())."<br>";
		$link = $URL."/html/gndy/china/list_4";
		$num = $this->get_page_num($link);
		$time1 = time();
		echo "抓取页面数量用时".($time1-$time)."s<br />";
		$type = 2;
		$res = $this->insert_content($link,$num,$type);
		//echo count($res)."<br />";
		$time2 = time();
		echo $time = "共用时".($time2-$time)."s";
		//print_r($res);
		//unset($res);
	}
	/*
	 * 执行第一步抓取所有电影数据的主函数
	 */
	public function add_film_info(){
		$time_start = time();
		$link_arr = array(
			array("http://www.ygdy8.net/html/gndy/dyzz/list_23",1),//最新电影
			array("http://www.ygdy8.net/html/gndy/china/list_4",2),//国内电影
			array("http://www.ygdy8.com/html/gndy/oumei/list_7",3),//欧美电影
			array("http://www.ygdy8.com/html/gndy/rihan/list_6",4),//日韩电影
			array("http://www.ygdy8.com/html/gndy/jddy/list_63",5)//综合电影
		);
		foreach($link_arr as $key=>$val){
			
			$num = $this->get_page_num($val[0]);
			$time1 = time();
			echo "抓取页面用时".($time1-$time_start)."s\n";
			$this->insert_content($val[0], $num, $val[1]);
		}
		$time_over = time();
		echo "总用时：".($time_over-$time_start)."s";
		
		
	}
	/*
	 *获取电影分页详细内容
	 * @param $link 传入的url
	 * @param $type 传入的电影类型 
	 */
	public function get_page_content($link,$type){
	//echo $link;
		if(isset($link)){
			
			$ch = curl_init();
			$url = $link;
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);
			curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
			$res = curl_exec($ch);
			curl_close($ch);
			
			$pattern = '/<a href="(.*)" class="ulink">(.*)<\/a>/';
			$pattern1 = '/《(.*)》/iU';
			$pattern2 = '/<font color="\#8F8C89">日期：(.*)<\/font>/iUs';
			//mb_convert_encoding($res, "UTF-8", "gb2312");	
			$res = iconv("gb2312", "UTF-8//IGNORE", $res);
			preg_match_all($pattern,$res,$res1);	
			preg_match_all($pattern2, $res,$res3);
			$res2 = array();
			$data=array();
			$num = count($res1[0]);
			$InsertInfo = D('FilmInfo');
			for($i = 0;$i < $num;$i++){
				$data['film_name'] = $res1[2][$i];
				preg_match($pattern1, $res1[2][$i],$film_name);
				$data['film_name_real'] = $film_name[1];
				$data['film_link'] = $res1[1][$i];
				$data['film_type'] = $type; //M('FilmInfo')->data($data)->add();
				$data['film_publish_time'] = strtotime(str_replace("点击：0", "", $res3[1][$i]));
				$InsertInfo->InsertFilmInfo($data);
			}
//			foreach($res1[2] as $key=>$val){
//				preg_match($pattern1,$val,$res2[$key]);
//				$res2[$key] = $res2[$key][1];
//				$data['film_name'] = $val;		
//			}
			
		}
		return true;
		
	}
	/*
	 * 
	 */
	public function insert_content($link,$num,$type){
		$link_arr = array();
		
		for($i = 1;$i<=$num;$i++){
			$time = time();
			$link_1 = $link."_".$i.".html";
			$this->get_page_content($link_1,$type);
			$time2 = time();
			echo "第 ".$i." 页用时：".($time2-$time)."s\n";
			
		}
		//return $link_arr;
		
		
	}
	/*
	 * 获取资源页数
	 * @param $link 传入的资源链接
	 */
	public function get_page_num($link){
		$ch = curl_init();
		$url = $link."_1.html";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
		$res = curl_exec($ch);
		$pattern = "/<option value='(.*)'>(.*)<\/option>/iUs";
		$result = preg_match_all($pattern, $res,$res1);
		curl_close($ch);
		$num = count($res1[2]);
		return $count = $res1[2][$num-1];
	}
//	public function insert_film_link($film_id,$link){
//		
////		$res = M("FilmInfo")->field("id,film_link")->select();
////		dump($res);
//		if(!empty($film_id)){
//			
//			$time_start = time();
//			$ch = curl_init();
//			echo $url = C("WEB_URL").$link;
//			//$url = "http://www.ygdy8.net/html/gndy/dyzz/20151201/49616.html";
//			curl_setopt($ch, CURLOPT_URL, $url);
//			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//			curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
//			$res = curl_exec($ch);
//			curl_close($ch);
//			$res = iconv("gb2312", "UTF-8//IGNORE", $res);
//			//echo $res;
//			$pattern = '/<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="(.*)">(.*)<\/a><\/td>/iUs';
//			$result = preg_match_all($pattern, $res,$res1);
//			//dump($res1);
//			D("FilmDownloadLink")->InsertFilmLink($film_id,$res1[1]);
//			$time_over = time();
//			echo "插入电影 ".$film_id." 用时：".($time_over-$time_start)."s<br />";
//		}else{
//			echo "无执行数据！";
//		}
//	}
	
	public function insert_film_link(){
		$time1 = time();
		$res = D("FilmInfo")->GetFilmLink();
		//dump($res);
		foreach($res as $key => $val){
			D("FilmDownloadLink")->insert_film_link($val['id'],$val['film_link']);
		}
		$time2 = time();
		echo "共用时：".($time2-$time1)."s\n";
		
	}
	public function insert_film_detail_from_douban(){
		$time1 = time();
		$name_arr = D('FilmInfo')->GetFilmName();
		foreach($name_arr as $key => $val){
			$time2 = time();
			$res = $this->get_film_detail($val['film_name_real']);
			$res['film_id'] = $val['id'];
			D('FilmDetail')->insert_film_detail($res);
			//var_dump($res);
			echo "抓取电影_".$val['id']." 用时".(time()-$time2)." s\n";
		}
		$time4 = time();
		echo "共用时：".($time4-$time1)."s\n";
		
	}
	public function get_film_detail($film_name){
		
		$ch = curl_init();
		$url = "http://movie.douban.com/subject_search?search_text=".$film_name;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		$res = curl_exec($ch);
		$pattern = '/<a class="nbg" href="http:\/\/movie.douban.com\/subject\/(.*)\/" onclick=(.*); title="(.*)">/iUs';
		preg_match($pattern, $res,$res1);
		curl_close($ch);
		
		$ch = curl_init();
		$url = "http://movie.douban.com/subject/".$res1[1]."/";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		$res = curl_exec($ch);
		$pattern = '/<div id="info">(.*)<\/div>/iUs';
		$pattern1 = '/<div id="interest_sectl">(.*)<\/span>
                <br \/>
            
    <\/div>/iUs';
		$pattern2 = '/<img src="http:\/\/img3.douban.com\/view\/movie_poster_cover\/spst\/public\/p(.*).jpg" title="点击看更多海报" alt="Pan" rel="v:image" \/>/iUs';
		$pattern3 = '/<span property="v:summary" class="">(.*)<\/span>/iUs';
		$pattern4 = '/<div class="rating_betterthan">(.*)<\/div>/iUs';
		$pattern5 = '/<strong class="ll rating_num" property="v:average">(.*)<\/strong>/iUs';
		$pattern6 = '/<span property="v:votes">(.*)<\/span>/';
		$pattern7 = '/<span class="pl">类型:<\/span> <span property="v:genre">(.*)<\/span>/iUs';
		preg_match($pattern, $res,$res1);//影片详细信息
		preg_match($pattern1, $res,$res2);//影片评分
//		preg_match($pattern2, $res,$res3);//图片
		preg_match($pattern3, $res,$res4);
		preg_match($pattern4, $res,$res5);
		preg_match($pattern5, $res,$res6);//评分
		preg_match($pattern6, $res,$res7);
		preg_match($pattern7, $res,$res8);
		curl_close($ch);
		//echo $res1[0][0];
//		dump($res1);
//		dump($res2);
//		dump($res4);
//		dump($res5);
		$film_info = array();
		$film_info['film_detail'] = $res1[0];
		$film_info['film_score_detail'] = $res2[0];
		$film_info['film_intro'] = $res4[0];
		$film_info['film_compare'] = $res5[0];
		$film_info['film_score'] = $res6[1];
		$film_info['film_vote'] = $res7[1];
		$film_info['film_type'] = $res8[1];
		
		//根据film_id从数据库中读取该部电影的封面链接和下载链接
		//$film_id = I('get.id');
		return $film_info;
		//dump($film_info);
		
	}

//多线程测试
	public function test1(){
		$time1 = time();
		$name_arr = D('FilmInfo')->GetFilmName();
		foreach($name_arr as $key => $val){
			$time2 = time();
			$res = $this->get_film_detail($val['film_name_real']);
			$res['film_id'] = $val['id'];
			D('FilmDetail')->insert_film_detail($res);
			//var_dump($res);
			echo "抓取电影_".$val['id']." 用时".(time()-$time2)." s\n";
		}
		$time4 = time();
		echo "共用时：".($time4-$time1)."s\n";
		
	}
	public function test(){
		$name_arr = D('FilmInfo')->GetFilmName();
		
		$this->curl_multi($name_arr);
	}
	public function curl_multi ($name_arr) {
		$time = time(); 
		$ch = curl_multi_init(); 
		$count = count($name_arr); 
		$ch_arr = array(); 
		for ($i = 0; $i < $count; $i++) { 
			$query_string = $name_arr[$i]['film_name_real']; 
			$url = 'http://movie.douban.com/subject_search?search_text='.$query_string;;
			$ch_arr[$i] = curl_init($url); 
			curl_setopt($ch_arr[$i], CURLOPT_RETURNTRANSFER, true); 
			curl_multi_add_handle($ch, $ch_arr[$i]); 
			echo $i." ";
		
		} 
		$running = null;
		 
		do { 
			curl_multi_exec($ch, $running); 
		} while ($running > 0); 
		for ($i = 0; $i < $count; $i++) {
			$time_s = time(); 
			$res[$i] = curl_multi_getcontent($ch_arr[$i]); 
			curl_multi_remove_handle($ch, $ch_arr[$i]); 
			//echo $ch_arr[$i]."\n";
			
			$pattern = '/<a class="nbg" href="http:\/\/movie.douban.com\/subject\/(.*)\/" onclick=(.*); title="(.*)">/iUs';
		preg_match($pattern, $res[$i],$res1[$i]);
		curl_close($ch);
		
		$ch = curl_init();
		$url = "http://movie.douban.com/subject/".$res1[$i][1]."/";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		$res[$i] = curl_exec($ch);
		
			$pattern = '/<div id="info">(.*)<\/div>/iUs';
		$pattern1 = '/<div id="interest_sectl">(.*)<\/span>
                <br \/>
            
    <\/div>/iUs';
		$pattern2 = '/<img src="http:\/\/img3.douban.com\/view\/movie_poster_cover\/spst\/public\/p(.*).jpg" title="点击看更多海报" alt="Pan" rel="v:image" \/>/iUs';
		$pattern3 = '/<span property="v:summary" class="">(.*)<\/span>/iUs';
		$pattern4 = '/<div class="rating_betterthan">(.*)<\/div>/iUs';
		$pattern5 = '/<strong class="ll rating_num" property="v:average">(.*)<\/strong>/iUs';
		$pattern6 = '/<span property="v:votes">(.*)<\/span>/';
		$pattern7 = '/<span class="pl">类型:<\/span> <span property="v:genre">(.*)<\/span>/iUs';
		preg_match($pattern, $res[$i],$res1[$i]);//影片详细信息
		preg_match($pattern1, $res[$i],$res2[$i]);//影片评分
//		preg_match($pattern2, $res,$res3);//图片
		preg_match($pattern3, $res[$i],$res4[$i]);
		preg_match($pattern4, $res[$i],$res5[$i]);
		preg_match($pattern5, $res[$i],$res6[$i]);//评分
		preg_match($pattern6, $res[$i],$res7[$i]);
		preg_match($pattern7, $res[$i],$res8[$i]);
		
		$film_info = array();
		$film_info[$i]['film_detail'] = $res1[$i][0];
		$film_info[$i]['film_score_detail'] = $res2[$i][0];
		$film_info[$i]['film_intro'] = $res4[$i][0];
		$film_info[$i]['film_compare'] = $res5[$i][0];
		$film_info[$i]['film_score'] = $res6[$i][1];
		$film_info[$i]['film_vote'] = $res7[$i][1];
		$film_info[$i]['film_type'] = $res8[$i][1];
		$film_info[$i]['film_id'] = $name_arr[$i]['id'];
		//dump($film_info);
		//echo "<hr>";	
		D('FilmDetail')->insert_film_detail($film_info[$i]);
		$time_o = time();
		echo "爬取电影_".$i." 用时".($time_o-$time_s)."s\n";
		} 
		echo "共用时：".(time()-$time)."s\n";
		curl_multi_close($ch); 
	//var_dump($results); 
}
	
	
}	
	
	
	
	
	
	
	
	
	
	
	
	
?>